package com.hippo.core.respository

import com.hippo.core.core.network.RetrofitBuilder
import com.hippo.core.core.network.response.ApiResponse
import com.hippo.core.core.network.safeApi
import com.hippo.core.dto.LoginDto
import com.hippo.core.dto.RegisterDto
import com.hippo.core.model.BasicData
import com.hippo.core.model.ConsumablesInfo
import com.hippo.core.model.Country
import com.hippo.core.model.Faq
import com.hippo.core.model.FaqDetail
import com.hippo.core.model.LastVersion
import com.hippo.core.model.PageBean
import com.hippo.core.model.Product
import com.hippo.core.model.ProductDetail
import com.hippo.core.model.UserInfo
import com.hippo.core.state.LoginState
import com.hippo.core.state.RegisterState
import com.hippo.core.state.ResetPwdState
import okhttp3.MediaType.Companion.toMediaTypeOrNull
import okhttp3.MultipartBody
import okhttp3.RequestBody.Companion.asRequestBody
import retrofit2.http.Query
import java.io.File

object ApiRepository {


    suspend fun loginOut(): ApiResponse<Any> {
        return safeApi {
            RetrofitBuilder.apiService.loginOut()
        }
    }

    suspend fun userOff(): ApiResponse<Boolean> {
        return safeApi {
            RetrofitBuilder.apiService.userOff()
        }
    }
    suspend fun userOffPC(password:String): ApiResponse<Boolean> {
        return safeApi {
            RetrofitBuilder.apiService.userOffPC(mapOf("password" to password))
        }
    }

    suspend fun changePassword(password: String): ApiResponse<Boolean> {
        return safeApi {
            RetrofitBuilder.apiService.changePassword(mapOf("password" to password))
        }
    }

    suspend fun login(params: LoginDto): ApiResponse<UserInfo> {
        return safeApi {
            RetrofitBuilder.apiService.login(params)
        }
    }


    suspend fun getUserInfo(): ApiResponse<UserInfo> {
        return safeApi {
            RetrofitBuilder.apiService.getUserInfo()
        }
    }


    suspend fun changeUserEmail(userEmail: String): ApiResponse<Boolean> {
        return safeApi {
            RetrofitBuilder.apiService.changeUserEmail(mapOf("userEmail" to userEmail))
        }
    }


    suspend fun changeNickName(nickName: String): ApiResponse<Boolean> {
        return safeApi {
            RetrofitBuilder.apiService.changeNickName(mapOf("nickName" to nickName))
        }
    }

    suspend fun changeAvatar(avatar: String): ApiResponse<Boolean> {
        return safeApi {
            RetrofitBuilder.apiService.changeAvatar(mapOf("avatar" to avatar))
        }
    }

    suspend fun resetPassword(params: ResetPwdState): ApiResponse<Boolean> {
        return safeApi {
            RetrofitBuilder.apiService.resetPassword(params)
        }
    }

    //"发送类型[可选参数,默认为0],1=注册邮件,2=找回密码邮件/修改密码，3=修改邮箱",
    suspend fun sendEmailCode(userEmail: String,sendType:String): ApiResponse<Boolean> {
        return safeApi {
            RetrofitBuilder.apiService.sendEmailCode(mapOf(
                "userEmail" to userEmail,
                "sendType" to sendType,
            ))
        }
    }
    suspend fun chkEmailCode(verifyCode: String): ApiResponse<Boolean> {
        return safeApi {
            RetrofitBuilder.apiService.chkEmailCode(mapOf(
                "verifyCode" to verifyCode,
            ))
        }
    }

    suspend fun userRegister(params: RegisterDto): ApiResponse<UserInfo> {
        return safeApi {
            RetrofitBuilder.apiService.userRegister(params)
        }
    }

    suspend fun getProductList(pageIndex:Int, pageSize:Int): ApiResponse<PageBean<Product>> {
        return safeApi {
            RetrofitBuilder.apiService.getProductList(pageIndex,pageSize)
        }
    }

    suspend fun getProductDetail(productId:Int): ApiResponse<ProductDetail> {
        return safeApi {
            RetrofitBuilder.apiService.getProductDetail(productId)
        }
    }


    suspend fun getProductFaqList(pageIndex:Int, pageSize:Int): ApiResponse<PageBean<Faq>> {
        return safeApi {
            RetrofitBuilder.apiService.getProductFaqList(pageIndex,pageSize)
        }
    }

    suspend fun getProductFaqDetail(faqId:Int): ApiResponse<FaqDetail> {
        return safeApi {
            RetrofitBuilder.apiService.getProductFaqDetail(faqId)
        }
    }

    suspend fun getCountryList(): ApiResponse<List<Country>> {
        return safeApi {
            RetrofitBuilder.apiService.getCountryList()
        }
    }


    suspend fun getBasicData(): ApiResponse<BasicData> {
        return safeApi {
            RetrofitBuilder.apiService.getBasicData()
        }
    }


    suspend fun getLastVersion(version:String): ApiResponse<LastVersion> {
        return safeApi {
            RetrofitBuilder.apiService.getLastVersion(version)
        }
    }


    suspend fun setFileUpload(path:String): ApiResponse<String> {
        return safeApi {
            val file = File(path)
            val requestFile = file.asRequestBody("multipart/form-data".toMediaTypeOrNull())
            val body = MultipartBody.Part.createFormData("fileParam", file.name, requestFile)
            RetrofitBuilder.apiService.setFileUpload(body)
        }
    }


    suspend fun getAndroidDownload(): ApiResponse<ProductDetail> {
        return safeApi {
            RetrofitBuilder.apiService.getAndroidDownload()
        }
    }
    suspend fun getConsumablesInfo(deviceId: String): ApiResponse<ConsumablesInfo> {
        return safeApi {
            RetrofitBuilder.apiService.getConsumablesInfo(deviceId)
        }
    }
    suspend fun resetDeviceWarn(deviceId: String,resetType:Int): ApiResponse<Boolean> {
        return safeApi {
            RetrofitBuilder.apiService.resetDeviceWarn(mapOf(
                "deviceId" to deviceId,
                "resetType" to resetType.toString(),
            ))
        }
    }

    suspend fun setDeviceWarn(deviceId:String): ApiResponse<Boolean> {
        return safeApi {
            RetrofitBuilder.apiService.setDeviceWarn(mapOf(
                "deviceId" to deviceId,
            ))
        }
    }





}